package com.zxp.quartz.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import lombok.SneakyThrows;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob1 implements Job {


	private String name;
	public String getName() {
		return name;
	}

	// name的值 ，可以通过JobDataMap的方式设置进去,见MyScheduler
	public void setName(String name) {
		this.name = name;
	}

	@SneakyThrows
	public void execute(JobExecutionContext context) throws JobExecutionException {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 拿到trigger里面的dataMap数据
        JobDataMap triggerDataMap = context.getTrigger().getJobDataMap();
        // 拿到配置的任务数据
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        // 合并后的dataMap，trigger会覆盖jobDetail里面的相同key
        JobDataMap mergedJobDataMap = context.getMergedJobDataMap();

/*		System.out.println("------JobDetail dataMap--------");
		dataMap.forEach((k,v)->System.out.println(k+":"+v));
		System.out.println("------Trigger dataMap--------");
		triggerDataMap.forEach((k,v)->System.out.println(k+":"+v));
		System.out.println("------Merged dataMap--------");
		mergedJobDataMap.forEach((k,v)->System.out.println(k+":"+v));*/

		//System.out.println("name:"+name);
		System.out.println( sf.format(date)+" " + System.identityHashCode(context.getJobInstance()) + " 任务执行了，" + name);
		Thread.sleep(3000L);

		// schedule执行任务的时候，会生成新的job实例,到下一个时间点，就会开启一个任务，是并发执行
		System.out.println( sf.format(new Date())+" " + System.identityHashCode(context.getJobInstance()) + " 任务执行完成");

    }
}
